package com.scitc.pleasurevideo.adapter;

import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.scitc.pleasurevideo.R;
import com.scitc.pleasurevideo.imgutil.ImageManager;
import com.scitc.pleasurevideo.pojo.DoubanTvVo;
import com.scitc.pleasurevideo.data.DataUtil;
import com.scitc.pleasurevideo.util.OnClickUtil;

import java.util.List;

public class RefreshListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mcontext;
    private List<DoubanTvVo> items;
    private int ORDINARY_VIEW = 1, FOOTER_VIEW = -100;

    //底部view
    private TextView tvMoreData;
    private View loadMoreDataIng;


    private int dataNumber = 0;
    private int pageSize = 3;

    public RefreshListAdapter(Context mcontext, List<DoubanTvVo> items) {
        this.mcontext = mcontext;
        this.items = items;
    }

    public void addItems(List<DoubanTvVo> items) {
        if (items == null || items.size() < 1) {
            return;
        }
        if (this.items == null || this.items.size() < 1) {
            resetItems(items);
            return;
        }
        this.items.remove(this.items.size() - 1);
        this.items.addAll(items);
        DoubanTvVo acticle = new DoubanTvVo();
        acticle.setId("-100");
        this.items.add(acticle);
        notifyDataSetChanged();
    }

    public void resetItems(List<DoubanTvVo> items) {
        if (items == null || items.size() < 1) {
            return;
        }
        this.items.clear();
        this.items.addAll(items);
        DoubanTvVo acticle = new DoubanTvVo();
        acticle.setId("-100");
        this.items.add(acticle);
        notifyDataSetChanged();
    }

    public DoubanTvVo getItemPosiion(int position) {
        if (position < 0 || position >= items.size()) {
            return null;
        }
        return items.get(position);

    }

    public List<DoubanTvVo> getItem() {
        if (items == null || items.size() < 1) {
            return null;
        }
        return items;
    }
    @Override
    public int getItemViewType(int position) {
        if (position < 0 || position >= items.size()) {
            return -1;
        } else if (position + 1 == items.size()) {
            return FOOTER_VIEW;
        }else {
            return position;
        }
    }
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        LayoutInflater layoutInflater = LayoutInflater.from(mcontext);
        if (i == FOOTER_VIEW) {
            View view = layoutInflater.inflate(R.layout.item_swip_footer, viewGroup, false);
            return new FooterViewHolder(view);
        }else {
            View view = layoutInflater.inflate(R.layout.item_tv_refresh_vertical, viewGroup, false);
            return new ViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        DoubanTvVo news = getItemPosiion(position);
        if(viewHolder instanceof ViewHolder){
            ViewHolder holder=(ViewHolder)viewHolder;
            if (news != null) {
                if (holder.titleText != null) {
                    holder.titleText.setText(news.getTitle());
                }

                if (holder.directorsText != null) {
                    holder.directorsText.setText("导演:" + DataUtil.list_string(news.getDirectors()));
                }

                if (holder.castsText != null) {
                    holder.castsText.setText("主演:" + DataUtil.list_string(news.getCasts()));
                }

                float rate_float;
                try {
                    rate_float = Float.valueOf(news.getRate());
                } catch (NumberFormatException e) {
                    rate_float = 0;
                }
                int rate_int = new Float(rate_float + 0.5).intValue();

                for (int i = 0; i < rate_int / 2; i++) {
                    holder.pentagram[i].setImageDrawable(ContextCompat.getDrawable(mcontext, R.mipmap.pentagram_all));
                }

                if (rate_int % 2 > 0) {
                    holder.pentagram[rate_int / 2].setImageDrawable(ContextCompat.getDrawable(mcontext, R.mipmap.pentagram_half));
                }

                if (holder.imageView != null) {

                    ImageManager.bindRoundedCornersImageCenterCrop(mcontext, holder.imageView, news.getCover(), 4);

                }

                holder.rateText.setText(news.getRate());
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    OnClickUtil.setViewClickTimes(view);
                    onClickItem.onClickItem(position);
                }
            });
            dataNumber++;
        } else if (viewHolder instanceof FooterViewHolder) {
            FooterViewHolder holder = (FooterViewHolder) viewHolder;
            if (dataNumber < pageSize) {
                setNoDataFooterViewUI();
            }
            dataNumber=0;
        }
    }

    /**
     * 当没有更多数据是展示
     */
    public void setNoDataFooterViewUI() {
        try {
            tvMoreData.setText("—页面有尽头，阅读无止境—");
            tvMoreData.setVisibility(View.VISIBLE);
            loadMoreDataIng.setVisibility(View.GONE);
        } catch (Exception e) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    try {
                        tvMoreData.setText("—页面有尽头，阅读无止境—");
                        tvMoreData.setVisibility(View.VISIBLE);
                        loadMoreDataIng.setVisibility(View.GONE);
                    } catch (Exception ignored) {
                    }
                }
            }, 300);
        }
    }

    /**
     * ‘
     * 还能加载跟多数据时的展示
     */
    public void setHaveDataFooterViewUI() {
        try {
            tvMoreData.setText("查看更多");
            tvMoreData.setVisibility(View.VISIBLE);
            loadMoreDataIng.setVisibility(View.GONE);
        } catch (Exception e) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    try {
                        tvMoreData.setText("查看更多");
                        tvMoreData.setVisibility(View.VISIBLE);
                        loadMoreDataIng.setVisibility(View.GONE);
                    } catch (Exception ignored) {
                    }
                }
            }, 300);
        }
    }

    /**
     * 正在加载更多时的展示
     */
    public void setLoadingDataFooterViewUi() {
        try {
            tvMoreData.setVisibility(View.GONE);
            loadMoreDataIng.setVisibility(View.VISIBLE);
        } catch (Exception e) {
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    try {
                        tvMoreData.setVisibility(View.GONE);
                        loadMoreDataIng.setVisibility(View.VISIBLE);
                    } catch (Exception ignored) {
                    }
                }
            }, 300);
        }
    }



    @Override
    public int getItemCount() {
        if (items == null || items.size() < 1) {
            return 0;
        }
        return items.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        ImageView imageView;
        ImageView[] pentagram = new ImageView[5];
        TextView titleText, directorsText, castsText, rateText;

        public ViewHolder(@NonNull View convertView) {
            super(convertView);
            imageView = convertView.findViewById(R.id.imageView);
            pentagram[0] = convertView.findViewById(R.id.pentagram1);
            pentagram[1] = convertView.findViewById(R.id.pentagram2);
            pentagram[2] = convertView.findViewById(R.id.pentagram3);
            pentagram[3] = convertView.findViewById(R.id.pentagram4);
            pentagram[4] = convertView.findViewById(R.id.pentagram5);
            titleText = convertView.findViewById(R.id.titleText);
            directorsText = convertView.findViewById(R.id.directorsText);
            castsText = convertView.findViewById(R.id.castsText);
            rateText = convertView.findViewById(R.id.rateText);
        }
    }
    /**
     * 底部-footer
     */
    class FooterViewHolder extends RecyclerView.ViewHolder {
        public FooterViewHolder(@NonNull View itemView) {
            super(itemView);
            tvMoreData = itemView.findViewById(R.id.tvMoreData);
            loadMoreDataIng = itemView.findViewById(R.id.loadMoreDataIng);
        }
    }

    private OnClickItem onClickItem;

    public interface OnClickItem {
        void onClickItem(int position);
    }

    public void setOnClickItem(OnClickItem onClickItem) {
        this.onClickItem = onClickItem;
    }
}
